// pages/new/new.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //banner轮播图
    list_banner: '',
    // 顶部导航
    none_box: false,
    top_btns: [
      { name: '2人团' },
      { name: '3人团' },
    ],
    group_content: [
      // { goods_id: '001', id: '1', img: '/pages/icon/u213.png', title: '骆驼户外抓绒衣骆驼户外抓绒衣', re_nums: '2', regiment_price: '99.00', sell_price: '299.00', },
    ],
    imgurl: app.urlimg,
    // 条件切换
    conditions: 0,
    scrollTop: 0,
    // 底部导航
    bottom_btns: [
      { img: '/pages/icon/group/11.png', text: '骆驼拼团' },
      { img: '/pages/icon/group/22.png', text: '抽奖团' },
      // { img: '/pages/icon/group/3.png', text: '免单团' },
    ],
    // 限制下拉触发一次
    go: true,
    defaultpages: 1,
  },
  /*条件切换 */
  switchs: function (e) {
    var that = this;
    that.setData({
      conditions: e.currentTarget.dataset.id,
      defaultpages: 1,
      scrollTop: 0,
    })
    that.load(that.data.conditions)
  },
  //头部导航
  top_click_two: function (e) {
    var that = this;
    that.setData({
      select_index: e.currentTarget.dataset.index,
      select_index_num: e.currentTarget.dataset.num,
      scrollTop: 0,
      defaultpages: 1,
    })
    console.log(e);
    console.log('发送请求');
    wx.request({
      url: app.url + 'api_team/timeBanner',
      data: {
        day: e.currentTarget.dataset.num,
        type: 1,
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token_key')
      },
      success: function (res) {
        that.setData({
          group_content: res.data.result.list,
          select_index: e.currentTarget.dataset.index,
        })
      },
    })
  },
  // banner点击
  photolink: function (e) {
    var that = this
    var url = e.currentTarget.dataset.url
    if (url !== '') {
      wx.redirectTo({ url: url })
    }
  },
  // 监听滑动事件
  scrollTop: function (e) {
    var that = this;
    var none_box = false;
    if (e.detail.scrollTop > 140) {
      none_box = true;
    }
    that.setData({
      none_box: none_box,
    })
  },
  // 进入详情
  particulars: function (e) {

    wx.navigateTo({
      url: '../../pages/group/groupdetail/index?id=' + e.currentTarget.dataset.index + '&regiment_id=' + e.currentTarget.dataset.regiment_id + '&group_type=' + e.currentTarget.dataset.group_type,
    })
  },
  /** 去开团 **/
  go_group: function (e) {
    console.log("去开团");
    wx.navigateTo({
      url: '../../pages/group/groupdetail/index?id=' + e.currentTarget.dataset.index + '&regiment_id=' + e.currentTarget.dataset.regiment_id,
    })
  },
  // 底部导航
  buttom_navigation_click: function (e) {
    var index = e.currentTarget.dataset.index;
    var url = "";
    if (index == 0) {
      url = "/pages/group/group";
    } else if (index == 1) {
      url = "/pages/group/lottery/lottery";
    } else if (index == 2) {
      url = "/pages/group/charge/index";
    }
    wx.redirectTo({
      url: url,
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      defaultpages: 1,   //显示页码
    })


    app.getUserInfo(function (token_key) {
      that.load(that.data.conditions);
      wx.request({
        url: app.url + 'api_banner/get_banner',
        data: {
          type: 2, // 1 限时抢购 2 拼团 3 抽奖团
        },
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token_key')
        }, success: function (res) {
          that.setData({
            list_banner: res.data.result,
          })
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;

    var page = ++this.data.defaultpages;
    // var that=this;
    var num = this.data.conditions;
    // // console.log(num)
    var re_nums = '';
    if (num == 0) {
      re_nums = 2
    } else if (num == 1) {
      re_nums = 3
    } else {
      re_nums = -1
    }

    if (that.data.go) {
      //that.setData({ go:false,})

      wx.request({
        // url: app.url + 'api_team/teamGoodsList',
        url: app.url + 'api_team/timeBanner',
        data: {
          page: page++,
          day: that.data.select_index_num,
          type: 1
        },
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token_key')
        },
        success: function (res) {
          console.log('下拉成功');
          if (res.data.result.list.length > 0) {
            var group_content = that.data.group_content;
            for (var i in res.data.result.list) {
              console.log(res.data.result.list[i]);
              group_content.push(res.data.result.list[i]);
            }
            console.log(group_content);
            that.setData({
              group_content: group_content,
              go: true,
              defaultpages: page,
            });
          } else {
            //提示信息
            wx.showToast({
              title: '已经到底部了',
              icon: 'loading',
              duration: 1200,
            })
            console.log("已经拉到底部了-------------------------------------------");
          }

        }, complete: function () {
          //显示出加载中的提示
          that.setData({ loadHidden: true })
        }

      })
    }
  },
  /*数据加载*/
  load: function (num) {
    console.log(num);
    var that = this;
    var re_nums = '';
    if (num == 0) {
      re_nums = 2
    } else if (num == 1) {
      re_nums = 3
    } else {
      re_nums = -1
    }
    //数据加载  头部样式为‘2人团’、‘3人团’
    // wx.request({
    //   url: app.url + 'api_team/teamGoodsList',
    //   data: {
    //     re_nums: re_nums,
    //   },
    //   header: {
    //     'content-type': 'application/json',
    //     'token': wx.getStorageSync('token_key')
    //   }, success: function (json) {
    //     console.log(json.data.result);

    //     //时间头部**********************************************************************
    //     //获取当前时间（用于页头的日期）
    //     var time_all = new Date();
    //     console.log(time_all);
    //     //获取三天前的时间
    //     time_all.setTime(time_all.getTime() - (24 * 60 * 60 * 1000) * 4);
    //     //计算天数函数
    //     function time_method() {
    //       time_all.setTime(time_all.getTime() + (24 * 60 * 60 * 1000) * 1);
    //       return (time_all.getMonth() + 1) + "/" + time_all.getDate();
    //     }
    //     var day_num_all = [];
    //     for (var i = 0; i < 7; i++) {
    //       // day_num_all.push(time_method())
    //       day_num_all[i] = {};
    //       var time_method_time = time_method();
    //       day_num_all[i].date_nj = time_method_time;
    //       var timestamp1 = Date.parse(new Date(time_method_time));
    //       timestamp1 = timestamp1 / 1000;
    //       var timestamp2 = (new Date().getMonth() + 1) + '/' + new Date().getDate();
    //       timestamp2 = Date.parse(new Date(timestamp2)) / 1000;
    //       if (timestamp1 <= timestamp2) {
    //         day_num_all[i].state = '已开始';
    //       } else if (timestamp1 > timestamp2) {
    //         day_num_all[i].state = '即将开团';
    //       } else {
    //         day_num_all[i].state = '开团中';
    //       }
    //     }
    //     console.log(day_num_all);

    //     that.setData({
    //       group_content: json.data.result,
    // 			select_index: 0,
    //       day_num: day_num_all,
    //     })
    //     console.log(that.data.defaultpages);
    //   }
    // })


    //数据请求  头部样式为‘时间日期’
    var time_all = new Date();//获取当前时间
    that.load_fun(that, CurentTime(time_all));

  },
  //加载函数   头部样式为‘时间日期’
  load_fun: function (that, time_day, select_top_index) {

    wx.request({
      // url: app.url + 'api_group/lotteryGroup',
      url: app.url + 'api_team/timeBanner',
      data: {
        day: time_day,
        type: 1,
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token_key')
      }, success: function (res) {
        console.log(res.data.result);

        //时间头部**********************************************************************
        var banner_time = res.data.result.banner;
        var select_index = 0;
        if (banner_time != '' && banner_time != undefined) {
          var num_aaa = 0;
          for (var i = 0; i < banner_time.length; i++) {
            var aaa = "" + banner_time[i].time;
            banner_time[i].text_day = aaa.substring(4, 6) + '/' + aaa.substring(6, 8);

            if (parseInt(time_day) < parseInt(banner_time[i].time)) {
              banner_time[i].state = '即将开团';
              num_aaa++;
            } else if (parseInt(time_day) > parseInt(banner_time[i].time)) {
              banner_time[i].state = '已开始';
              num_aaa++;
            } else if (parseInt(time_day) == parseInt(banner_time[i].time)) {
              banner_time[i].state = '开团中';
              select_index = i;
            }
          }
        }

        if (res.data.result.list == '') {
          wx.request({
            url: app.url + 'api_team/timeBanner',
            data: {
              day: (banner_time != '' ? banner_time[0].time : '0'),
              type: 1,
            },
            header: {
              'content-type': 'application/json',
              'token': wx.getStorageSync('token_key')
            },
            success: function (res) {
              that.setData({
                group_content: res.data.result.list,
                select_index: 0,
              })
            },
          })
        } else {
          that.setData({
            group_content: res.data.result.list,
          })
        }

        console.log(banner_time);
        that.setData({
          day_num: banner_time,
          select_index_num: (banner_time != '' ? banner_time[0].time : ''),
          select_index: select_index,
        })
        // var list = res.data.result.list;
        // for (var i = 0; i < list.length; i++) {
        //   //获得后台转过来的毫秒数 储存在time_box中
        //   // that.data.time_all.push(list[i].remain_time)
        //   that.data.time_all.push(list[i].timeout)
        //   // 获得毫秒数  转化  成小时、分钟、秒
        //   // that.time_cipher(list[i].remain_time, i);
        //   that.time_cipher(list[i].timeout, i);
        // }
      }

    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  autoSubmit: function (e) {
    wx.request({
      url: app.url + "api_team/collectFormId",
      data: {
        formId: e.detail.formId
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token_key')
      },
      success: function (result) { }
    })
  }
})

//转换时间格式结构
function CurentTime(now) {
  var year = now.getFullYear();       //年
  var month = now.getMonth() + 1;     //月
  var day = now.getDate();            //日

  var clock = year + "";
  if (month < 10)
    clock += "0";
  clock += month + "";
  if (day < 10)
    clock += "0";
  clock += day + "";

  return (clock);
}